
package Utilities;

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;


/* Start of 'AlertDialog' class */
public class AlertDialog implements CommandListener
{
  public Alert Alt = new Alert("Alert");
  public final Command OK_ALERT_COMMAND =
        new Command("Ok", Command.OK, 0);
  public final Command CANCEL_ALERT_COMMAND =
        new Command("No", Command.CANCEL, 0);
  public boolean IsCritical = false;
  public Runnable ConfYes, ConfNo;

  public static AlertDialog AltD = null;

  /* 'AlertDialog' class constructor
   * ARGUMENTS: None.
   * RETURNS: None.
   */
  private AlertDialog(  )
  {
  } /* End of 'AlertDialog' class constructor */

  /* Initialize alert system function
   * ARGUMENTS: None.
   * RETURNS: None.
   */
  private static void Init( )
  {
    AltD = new AlertDialog();
    AltD.Alt.addCommand(AltD.OK_ALERT_COMMAND);
    AltD.Alt.setCommandListener(AltD);
  } /* End of 'Init' function */

  /* Show error alert dialog function
   * ARGUMENTS:
   *   - error message
   *       String Msg;
   *   - is midlet need to be closed
   *       boolean Critical;
   * RETURNS: None.
   */
  public static void ShowError( String Msg, boolean Critical )
  {
    if (AltD == null)
      Init();
    AltD.IsCritical = Critical;
    AltD.Alt.setTitle("Error!");
    AltD.Alt.setString(Msg);
    AltD.Alt.setType(AlertType.ERROR);
    AltD.Alt.setTimeout(Alert.FOREVER);

    MyDisplay.PushDisplayable();
    MyDisplay.SwitchDisplayable(null, AltD.Alt);
    
  } /* End of 'ShowError' function */

  /* Show warning alert dialog function
   * ARGUMENTS:
   *   - warning message
   *       String Msg;
   * RETURNS: None.
   */
  public static void ShowWarning( String Msg )
  {
    if (AltD == null)
      Init();
    AltD.Alt.setTitle("Warning!");
    AltD.Alt.setString(Msg);
    AltD.Alt.setType(AlertType.WARNING);
    AltD.Alt.setTimeout(4000);

    MyDisplay.PushDisplayable();
    MyDisplay.SwitchDisplayable(null, AltD.Alt);
  } /* End of 'ShowWarning' function */

  /* Show confirmation alert dialog function
   * ARGUMENTS:
   *   - confirmation message
   *       String Msg;
   *   - to do if confirmation accepted
   *       Runnable Yes;
   *   - to do if confirmation declined
   *       Runnable No;
   * RETURNS: None.
   */
  public static void ShowConfirmation( String Msg, Runnable Yes, Runnable No )
  {
    if (AltD == null)
      Init();
    AltD.Alt.setTitle("Confiramtion.");
    AltD.Alt.setString(Msg);
    AltD.Alt.setType(AlertType.CONFIRMATION);
    AltD.Alt.setTimeout(Alert.FOREVER);
    AltD.Alt.addCommand(AltD.CANCEL_ALERT_COMMAND);

    AltD.ConfYes = Yes;
    AltD.ConfNo = No;
   
    MyDisplay.PushDisplayable();
    MyDisplay.SwitchDisplayable(null, AltD.Alt);
  } /* End of 'ShowConfirmation' function */

  /* Show confirmation alert dialog function
   * ARGUMENTS:
   *   - title of information dialog
   *       String Title;
   *   - information message
   *       String Msg;
   * RETURNS: None.
   */
  public static void ShowInfo( String Title, String Msg )
  {
    if (AltD == null)
      Init();
    AltD.Alt.setTitle(Title);
    AltD.Alt.setString(Msg);
    AltD.Alt.setType(AlertType.INFO);
    AltD.Alt.setTimeout(4000);

    MyDisplay.PushDisplayable();
    MyDisplay.SwitchDisplayable(null, AltD.Alt);
  } /* End of 'ShowInfo' function */
  
  /* Show alarm alert dialog function
   * ARGUMENTS:
   *   - title of alarm dialog
   *       String Title;
   *   - alarm message
   *       String Msg;
   * RETURNS: None.
   */
  public static void ShowAlarm( String Title, String Msg )
  {
    if (AltD == null)
      Init();
    AltD.Alt.setTitle(Title);
    AltD.Alt.setString(Msg);
    AltD.Alt.setType(AlertType.ALARM);
    AltD.Alt.setTimeout(4000);

    MyDisplay.PushDisplayable();
    MyDisplay.SwitchDisplayable(null, AltD.Alt);
  } /* End of 'ShowAlarm' function */


  /* Alert dialog messsages perfomance function
   * ARGUMENTS:
   *   - alert dialog command
   *       Command c;
   *   - dialog from this command is
   *       Displayable d;
   * RETURNS: None.
   */
  public void commandAction( Command c, Displayable d )
  {
    MyDisplay.PopDisplayable();
    if (AltD.Alt.getType() == AlertType.ERROR)
    {
      if (c == AltD.OK_ALERT_COMMAND)
        if (IsCritical == true)
        {
          IsCritical = false;
          System.exit(1);
        }
    }
    else if(AltD.Alt.getType() == AlertType.CONFIRMATION)
    {
      AltD.Alt.removeCommand(AltD.CANCEL_ALERT_COMMAND);
      if (c == AltD.OK_ALERT_COMMAND)
      {
        if (AltD.ConfYes != null)
          AltD.ConfYes.run();
      }
      else if (c == AltD.CANCEL_ALERT_COMMAND)
        if (AltD.ConfNo != null)
          AltD.ConfNo.run();
    }
    else if(AltD.Alt.getType() == AlertType.INFO)
    {
      if (c == AltD.OK_ALERT_COMMAND)
        ;
    }
  } /* End of 'CommandAction' function */
} /* End of 'AlertDialog' class */
